package com.sxdz.controller.service

import com.toolbox.utils.ClearNullUtil
import com.sxdz.controller.mapper.IBaseMapper
import com.sxdz.models.common.Order
import com.sxdz.models.common.RollPage
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * Created with IntelliJ IDEA.
 * User: 张斌
 * Date: 2014/11/18
 */
@CompileStatic
@TypeChecked
abstract class BaseService implements IBaseService{
    Logger logger = LoggerFactory.getLogger(BaseService.getClass())
    protected Integer pageSizeDefault = 20

    protected IBaseMapper baseMapper

    void setBaseMapper(IBaseMapper baseMapper) {
        this.baseMapper = baseMapper
    }

    abstract Long addBasic(def obj) throws Exception

    abstract void modifyBasic(def obj) throws Exception

    abstract void delBasic(def obj) throws Exception

    @Override
    Object findObjByKey(def Object seq) throws Exception {
        baseMapper.selectByPrimaryKey(seq)
    }

    @Override
    Object findObj(Map<String, Object> params) throws Exception {
        ClearNullUtil.mapClear(params)
        baseMapper.selectByParams(params)
    }

    @Override
    List findListByParams(Map<String, Object> params, Order order) throws Exception {
        ClearNullUtil.mapClear(params)
        baseMapper.selectListByParams(params,null,null,order?.toString())
    }



    @Override
    Integer findCountByParams(Map<String, Object> params) {
        return baseMapper.selectCountByParams(params)
    }

    @Override
    List findListByParams(Map<String, Object> params, Order order, Integer maxRecord) throws Exception {
        ClearNullUtil.mapClear(params);
        logger.info("**************params="+params+
                ";maxRecord="+maxRecord+";order="+order.toString());
        baseMapper.selectListByParams(params, 0, maxRecord, order?.toString())
    }

    @Override
    RollPage findListPageByParams(Map<String, Object> params, Order order, Integer pageNum, Integer pageSize) throws Exception {
        ClearNullUtil.mapClear(params)
        Integer recordSum= baseMapper.selectCountByParams(params)
        //Integer pageSizeCustom = ManageConfigCache.getConfigByConfigCode("admin.pagesize").configValue as Integer
        Integer pageSizeCustom = this.pageSizeDefault
        RollPage rollPage=new RollPage(iTotalRecords:recordSum,iTotalDisplayRecords:recordSum,pageSize:pageSize?:pageSizeCustom,pageNum: pageNum?:1)

        Integer pageOffset=(rollPage.getPageNum() - 1) * rollPage.getPageSize()

        if (recordSum>0) {
            rollPage.recordList =baseMapper.selectListByParams(params, pageOffset, rollPage.pageSize, order?.toString())
        }
        else{
            rollPage.recordList = []
        }

        rollPage;
    }


}
